### Project 4 Infrared remote control smart car ![](media/wps11.png) **1.Before the experiment** 1. Place the function library IRremote into the Arduino libraries directory. 2. Open IrReceive.pde to acquire the code for your infrared remote control ( IRcode will be displayed in Serial Monitor), write down IRcode and modify it in your IR code in the program. Code 5 ```c /* *IRRemote code test *Example 1.2: display the type of IR protocol such as NEC, Sony SIRC, Philips RC5, Philips RC6 */ #include // call IRRemote function library const int irReceiverPin = 2; // set pin 2 as IR receiver signal OUTPUT IRrecv irrecv(irReceiverPin); // set IRrecv to receive IR signal decode_results results; // decode result will be put into the variable of the result in the decode_results void setup() { Serial.begin(9600);// set communication rate at 9600 bps irrecv.enableIRIn();// start IR decoding } // display the type of IR protocol void showIRProtocol(decode_results *results) { Serial.print("Protocol: "); // determine the type of IR protocol switch(results->decode_type) { case NEC: Serial.print("NEC"); break; case SONY: Serial.print("SONY"); break; case RC5: Serial.print("RC5"); break; case RC6: Serial.print("RC6"); break; default: Serial.print("Unknown encoding"); } // serial print IR code to Serial port Serial.print(", irCode: "); Serial.print(results->value, HEX); // IR code Serial.print(", bits: "); Serial.println(results->bits); // IR code bit } void loop() { if (irrecv.decode(&results)) { // finish decoding, receive IR signal showIRProtocol(&results);// display the type of IR protocol irrecv.resume();// continue receiving IR signal coming next } } ``` Replace the IR code in the IR control part program with the code from the test result IR remote control smart car program. ![](media/wps12.png) **2.IR remote control smart car program** Code 6 ```c #include int RECV_PIN = 2; int pinLB=5;// define pin for I1 int pinLF=6;// define pin for I2 int pinRB=10;// define pin for I3 int pinRF=11;// define pin for I4 //******IR control part******** long advence = 0x00FF629D; long back = 0x00FFA857; long stop = 0x00FF02FD; long left = 0x00FF22DD; long right = 0x00FFC23D; IRrecv irrecv(RECV_PIN); decode_results results; void dump(decode_results *results) { int count = results->rawlen; if (results->decode_type == UNKNOWN) { Serial.println("Could not decode message"); } else { if (results->decode_type == NEC) { Serial.print("Decoded NEC: "); } else if (results->decode_type == SONY) { Serial.print("Decoded SONY: "); } else if (results->decode_type == RC5) { Serial.print("Decoded RC5: "); } else if (results->decode_type == RC6) { Serial.print("Decoded RC6: "); } Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): "); for (int i = 0; i < count; i++) { if ((i % 2) == 1) { Serial.print(results->rawbuf[i]*USECPERTICK, DEC); } else { Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); } Serial.print(" "); } Serial.println(""); } void setup() { pinMode(RECV_PIN, INPUT); pinMode(pinLB,OUTPUT); pinMode(pinLF,OUTPUT); pinMode(pinRB,OUTPUT); pinMode(pinRF,OUTPUT); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } int on = 0; unsigned long last = millis(); void loop() { if (irrecv.decode(&results)) { // If it's been at least 1/4 second since the last // IR received, toggle the relay if (millis() - last > 250) { on = !on; // digitalWrite(8, on ? HIGH : LOW); digitalWrite(13, on ? HIGH : LOW); dump(&results); } if (results.value == advence ) { digitalWrite(pinRB,LOW);// motor going right digitalWrite(pinRF,HIGH); digitalWrite(pinLB,LOW);// motor going left digitalWrite(pinLF,HIGH); } if (results.value == back ) { digitalWrite(pinRB,HIGH);// motor going right and BACK digitalWrite(pinRF,LOW); digitalWrite(pinLB,HIGH);// motor going left and BACK digitalWrite(pinLF,LOW); } if (results.value == left ) { digitalWrite(pinRB,LOW);// motor going right and STOP digitalWrite(pinRF,HIGH); digitalWrite(pinLB,HIGH);// motor going left digitalWrite(pinLF,LOW); } if (results.value == right ) { digitalWrite(pinRB,HIGH);// motor going right digitalWrite(pinRF,LOW); digitalWrite(pinLB,HIGH);// motor going left and STOP digitalWrite(pinLF,HIGH); } if (results.value == stop ) { digitalWrite(pinRB,HIGH);// motor going right and STOP digitalWrite(pinRF,HIGH); digitalWrite(pinLB,HIGH);// motor going left and STOP digitalWrite(pinLF,HIGH); } last = millis(); irrecv.resume(); // Receive the next value } } ```